<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table   @row-dblclick="getDrawer" :data="SystemDicTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="数据字典类型名" width="300" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="数据字典编号" width="500"  sortable>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,sizes,prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total"
                     :page-sizes="[3, 4, 5, 10]"
                     @size-change = "handlePageSize"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title"  :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="类型" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!-- 抽屉新增或编辑模态框-->
    <el-dialog :title="title"  :visible.sync="drawerFormVisible" :close-on-click-modal="false">
      <el-form :model="drawerForm" label-width="80px" :rules="drawerFormRules" ref="drawerForm">
        <el-form-item label="名字" prop="name">
          <el-input v-model="drawerForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="name">
          <el-select v-model="drawerForm.types" value-key="id" placeholder="请选择">
            <!--
         :key：必须指定为你每条数据中的唯一字段
         :label：用户选择之后在下拉框中展示的值
         :value：用户选中值之后要提交时，使用那个字段提交
       -->
            <el-option
                v-for="item in detail"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="drawerFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="drawerSubmit" :loading="drawerLoading">提交</el-button>
      </div>
    </el-dialog>
    <!-- 抽屉代码-->
    <el-drawer
        title="数据字典明细!"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="gridData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="300" label="编号">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="200"  sortable>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="drawerAdd(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="drawerEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="drawerDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="drawerBatchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="total,sizes,prev, pager, next"
                       @current-change="drawerCurrentChange"
                       :page-size="pageSize"
                       :current-page="currentPage"
                       :total="total"
                       :page-sizes="[3, 4, 5, 10]"
                       @size-change = "drawerPageSize"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getSystemDicTypeListPage, removeSystemDicType, batchRemoveSystemDicType, editSystemDicType, addSystemDicType } from '../../api/api';

	export default {
		data() {
			return {
        detail:[],
        table: false,
        gridData: [],
				filters: {
          keyword: ''
				},
				SystemDicTypes: [],
        dept:[],
        login:[],
        shops:[],
        title:"",
				total: 0,
        currentPage: 1,
        pageSize: 3,
				listLoading: false,
				sels: [],//列表选中列
        id:null,
        types_id:0,

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
          name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
          sn: [
						{ required: true, message: '请输入编号', trigger: 'blur' }
          ]
				},
				//编辑界面数据
				editForm: {
          id:null,
          name:"",
          sn:""
				},
        drawerFormVisible: false,//编辑界面是否显示
        drawerLoading: false,
        drawerFormRules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
        },
        drawerForm: {
          id:null,
          name:"",
          sn:"",
          types:[]
        },
			}
		},
		methods: {
      //选择每页展示条数
      drawerPageSize(val){
        this.currentPage = 1;
        this.pageSize = val;
        this.drawerData();
      },
      //点击下一页刷新数据
      drawerCurrentChange(val) {
        this.currentPage = val;
        this.drawerData();
      },
      //获取抽屉内层列表
      getDrawer(row) {
        //每次子表打开，页码重新赋值为1
        this.currentPage = 1
        //每次子表打开，每页数据条数重新赋值为5条
        this.pageSize = 3
        //品牌id赋值
        this.types_id = row.id
        //展示内层抽屉列表
        this.drawerData();
        //弹出内层抽屉
        this.table = true;

      },

      drawerData(){
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword,
          id : this.types_id
        };
        this.listLoading = true;
        this.$http.post("/dicdetail",para)
            .then(result=>{
              this.table = true;
              this.listLoading = false;
              this.gridData = result.data.rows;

              this.total = result.data.total;
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message.error("系统繁忙,请骚后重试");
              console.log(result)
            })
      },
      //选择每页展示条数
      handlePageSize(val){
        this.currentPage = 1;
        this.pageSize = val;
        this.getSystemDicTypes();
      },
      //点击下一页刷新数据
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getSystemDicTypes();
      },
			//高级查询需要定位到第一页
      search(){
        this.currentPage = 1;
        this.getSystemDicTypes();
      },
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},

			//获取用户列表
			getSystemDicTypes() {
				let para = {
					currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
				this.$http.post("/dictype",para)
        .then(result=>{
          this.listLoading = false;
            this.detail = result.data.rows;
            this.SystemDicTypes = result.data.rows;
            this.total = result.data.total;
        })
        .catch(result=>{
          this.listLoading = false;
          this.$message.error("系统繁忙,请骚后重试");
          console.log(result)
        })
			},
			//删除
      drawerDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dicdetail/"+row.id)
            .then(result=>{
              if(result.data.success){
                this.listLoading = false;
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'warning'
                });
              }
              this.currentPage = 1;
              this.drawerData();
            })
            .catch(result=>{
              this.$message({
                message: "删除失败",
                type: 'error'
              });
            })
				}).catch(() => {

				});
			},
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/dictype/"+row.id)
              .then(result=>{
                if(result.data.success){
                  this.listLoading = false;
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'warning'
                  });
                }
                this.currentPage = 1;
                this.getSystemDicTypes();
              })
              .catch(result=>{
                this.$message({
                  message: "删除失败",
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
      //显示抽屉编辑界面
      drawerEdit: function (index, row) {
        this.drawerFormVisible = true;
        this.drawerForm = Object.assign({}, row);
        this.title = "编辑"
        this.drawerForm.types_id = this.types_id
        this.$http.get("/dictype/"+this.drawerForm.types_id)
          .then(result=>{
              this.drawerForm.types = result.data;
          })
        console.log(this.drawerForm)
      },
      //显示抽屉新增界面
      drawerAdd: function () {
        this.title = "新增"
        this.drawerFormVisible = true;
        this.drawerForm = {
          id:null,
          name:"",
          sn:"",
          types:[]
        };
      },
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
        this.title = "编辑"
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增"
				this.editFormVisible = true;
				this.editForm = {
          id:null,
          name:"",
          sn:""
				};
			},
			//编辑or新增
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
              this.$http.put("/dictype",para)
                .then(result=>{
                  this.editLoading = false;
                  this.editFormVisible = false;
                  if(result.data.success){
                    this.$message({
                      message: result.data.message,
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: result.data.message,
                      type: 'warning'
                    });
                  }
                  this.getSystemDicTypes();
                })
                .catch(result=>{
                  this.$message({
                    message: "新增/编辑失败",
                    type: 'error'
                  });
                })
						});
					}
				});
			},
      //抽屉编辑or新增
      drawerSubmit: function () {
        this.$refs.drawerForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.drawerLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.drawerForm);
              this.$http.put("/dicdetail",para)
                  .then(result=>{
                    this.drawerLoading = false;
                    this.drawerFormVisible = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'warning'
                      });
                    }
                    this.drawerData();
                  })
                  .catch(result=>{
                    this.$message({
                      message: "新增/编辑失败",
                      type: 'error'
                    });
                  })
            });
          }
        });
      },
			selsChange: function (sels) {
				this.sels = sels;
			},
      //抽屉批量删除
      drawerBatchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/dicdetail",ids)
              .then(result=>{
                this.listLoading = false;
                if (result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'warning'
                  });
                }
                this.currentPage = 1;
                this.drawerData();
              })
              .catch(result=>{
                this.$message({
                  message: "批量删除GG",
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/dictype",ids)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'warning'
                });
              }
              this.currentPage = 1;
              this.getSystemDicTypes();
            })
            .catch(result=>{
              this.$message({
                message: "批量删除GG",
                type: 'error'
              });
            })
				}).catch(() => {

				});
			}
		},
		mounted() {
      //获取数据字典类型
			this.getSystemDicTypes();
		}
	}

</script>

<style scoped>

</style>